﻿' This file explains how you should use the Compiler tool to compile
' and solve expressions without variables.

' 1. Add a reference to the Enhancements.Basic assembly.
' 2. Create an instance of the Enhancements.Basic.Expressions.Compiler class,
'    indicating which functions will be made available to the compiler.
' 3. Call the compiler's Compile method in a try/catch block as the expression can be invalid.
'    If valid, an Expression instance is returned.
' 4. Call the Expression instance' Solve method in a try/catch block as the expression solving
'    can result in an exception. If all well, the expression's result is returned.

Imports Enhancements.Basic.Expressions

Public Class CompileAndSolveExpressionsWithoutVariables

    Private __Compiler As Compiler = Nothing

    Public Sub New()
        __Compiler = New Compiler(True, True, True, True, True, True, True, New Functions.Function() {})
    End Sub

    Public Function CompileAndSolve(ByVal _expressionText As String) As Object

        Dim _compiledExpression As Expression = Nothing
        Dim _expressionResult As Object = Nothing

        Try
            _compiledExpression = __Compiler.Compile(_expressionText)

        Catch _exception As Exception
            'The compilation process went wrong! Throw it back to the client.
            Throw New ApplicationException("Compilation generated an error.", _exception)
        End Try

        Try
            _expressionResult = _compiledExpression.Solve()

        Catch _exception As Exception
            'The solving process went wrong! Throw it back to the client.
            Throw New ApplicationException("Solving generated an error.", _exception)
        End Try

        Return _expressionResult

    End Function

End Class
